import { Address } from "cluster";


//注意要使用require
const Index = require('../build/contracts/index.json')
const Product = require('../build/contracts/product.json')
const User = require("../build/contracts/User.json")
const Web3 = require("web3")

const localhost = "http://127.0.0.1:7545";
const provider = new Web3.providers.HttpProvider(localhost);
const web3 = new Web3(provider);
const indexAddress = "0x6DC963955E9fa51c446112E47B2665AaD17EABb5";
interface User {
    name: string;
    password: string;
}


async function getUser() {
    const accounts = await web3.eth.getAccounts();
    const userAdd = "0x5B43b2337327ef6C2d3E750ae1a6D9cEd16e2651";
    const user = new web3.eth.Contract(User.abi as any, userAdd)
    const info = await user.methods.getDetail().call();
    const message = await user.methods.message().call();
    await user.methods.pushProduct("0xdC2Aa5108E347149f0887BA14993adDd6088e6C1")
    .send({from:accounts[0],gas:"5000000"});
    const list = await user.methods.getProducts().call();
  
    console.log("info:", info);
    console.log("message:", message);
    console.log("prolsit:", list);


}
async function createPro(add) {
    const proaddress = "0xdC2Aa5108E347149f0887BA14993adDd6088e6C1";
    const accounts = await web3.eth.getAccounts();
    // const contracts = new web3.eth.Contract(Product.abi);
    // const deploy = contracts.deploy({
    //     data: Product.bytecode,
    //     arguments: [
    //         "Product Name",
    //         "Product Description",
    //         "Made Address",
    //         "Company Name",
    //         "Made Time",
    //         "Applicant",
    //         add // 传入合约拥有者的地址
    //     ]
    // });

    // const result = await deploy.send({
    //     from: accounts[0],
    //     gas: '5000000'
    // });

    // console.log(result.options.address);
    const pro = await new web3.eth.Contract(Product.abi as any, proaddress)
    const collect = await pro.methods.getCollect().call();
    const basic =await pro.methods.getBasic().call();
    console.log("basic:",basic);
    
    console.log("collect:", collect);
    const aru = {time:" _time",
       address: " _collectAddress",
        op:" _operator",
        id:" _imageId",
        message:" _message"}
    await pro.methods.setCollect(aru.time,aru.address,aru.op,aru.id,aru.message).send({from:accounts[0],gas:"5000000"});
    const newcollect = await pro.methods.getCollect().call();
    console.log("newcollect:", newcollect);

}
async function test1() {


    const accounts = await web3.eth.getAccounts();
    console.log(accounts[0]);

    const payload: any = ["8888", "7777"];
    // const test=await new web3.eth.Contract(User.abi as any)
    //     .deploy({data:User.bytecode,arguments:payload})
    //     .send({
    //         from:accounts[0],
    //         gas:"5000000"
    //     })
    // //实例地址
    // console.log(test.options.address)
    const index = new web3.eth.Contract(Index.abi as any, indexAddress)
    await index.methods.pushProduct("0xdC2Aa5108E347149f0887BA14993adDd6088e6C1")
    .send({from:accounts[0],gas:"5000000"});
    const list = await index.methods.getAllUsers().call();
    console.log("indexlist:", list);
    // getUser();
    createPro("0x5B43b2337327ef6C2d3E750ae1a6D9cEd16e2651");
    getUser();
}
test1()
